package it.lupo.model.service;

import it.lupo.core.gui.controller.ConfermentTableRow;
import it.lupo.model.BarCode;
import it.lupo.model.Conferment;
import it.lupo.model.Type;
import it.lupo.model.User;
import it.lupo.repository.HibernateSessionFactory;
import it.lupo.repository.dao.ConfermentDAO;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class ConfermentService {
	
	private static ConfermentService instance;
	private ConfermentService(){}
	
	public static ConfermentService getInstance()
	{
		if(instance==null)
			instance=new ConfermentService();
		return instance;
	}
	
	 
	
//	public static void main(String []args)
//	{
//		BarCode bc = new BarCode("123456", "GRCDNC86M21G786X", "Domenico","Grieco","Via Bottalico","",true);
//		BarCodeService.getInstance().create(bc);
//	}
	
	public void create(BarCode barcode,User operator,List<ConfermentTableRow> rows)
	{
		Session session = HibernateSessionFactory.getSession();
		Transaction trx = session.beginTransaction();
		try{		
			ConfermentDAO cdao = new ConfermentDAO();
			int k=1;
			Long id  =Calendar.getInstance().getTimeInMillis();
			for(ConfermentTableRow r:rows)
			{			
				Conferment cnf = new Conferment(id,k++,new Date(), operator, barcode, new Type(r.getType()), r.getWeight(), "KG", false);
				cdao.create(cnf);
			}
			
			trx.commit();
			session.close();
		}catch(Exception e)
		{
			e.printStackTrace();
			trx.rollback();	
			session.close();
		}
	}

//	public Conferment create(BarCode barcode,TYPE type,User operator,double weight) {
//		Session session = HibernateSessionFactory.getSession();
//		Transaction trx = session.beginTransaction();
//		
//		ConfermentDAO udao = new ConfermentDAO();
//		Conferment cnf = new Conferment(new Date(), operator, barcode, new Type(type.toString()), weight, "KG", false);
//		udao.create(cnf);
//		
//		trx.commit();
//		session.close();
//		return cnf;
//		
//	}
}
